package com.ms.qw.application.receta;

import com.ms.qw.application.mappers.CampanaMapper;
import com.ms.qw.application.mappers.ComunMapper;
import com.ms.qw.application.mappers.RecetaMapper;
import com.ms.qw.client.dto.CampanaDTO;
import com.ms.qw.client.dto.ProgramacionDTO;
import com.ms.qw.client.dto.RecetaDTO;
import com.ms.qw.client.dto.RecetaDetalleDTO;
import com.ms.qw.domain.model.*;
import com.ms.qw.domain.support.ValidationNotificationHandler;
import com.ms.qw.server.infrastructure.persistence.exception.ApplicationException;
import com.ms.support.mapper.DTOMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RegistroCampanaAppImpl implements RegistroCampanaApp {

    @Autowired
    DTOMapperService mapperService;

    @Autowired
    CampanaRepository campanaRepository;

    @Autowired
    CampanaMapper campanaMapper;

    @Autowired
    ComunMapper comunMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(Campana campana, boolean updateMode) {

        ValidationNotificationHandler handler = new ValidationNotificationHandler();
        ValidatorCampana validatorCampana
                = new ValidatorCampana(campana, handler);
        validatorCampana.validate();
        if (handler.getValidationItems().size() != 0) {
            throw new ApplicationException(handler.getValidationItems().get(0).getDescription());
        }

        if (updateMode) {
            campanaRepository.merge(campana);
        } else {
            campanaRepository.persist(campana);
        }
    }


    public CampanaDTO findById(Integer codigo) {
        CampanaDTO campana= campanaRepository.findById(codigo);
        CampanaDTO campanaReferencia= new CampanaDTO();

        campanaReferencia.setCodigo(campana.getCodigoCampanaReferencia());
        campanaReferencia.setNombre(campana.getNombreCampanaReferencia());
        campana.setCampanaReferencia(campanaReferencia);
        return campana;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void delete(Integer codigo) {
        campanaMapper.inactivar(codigo);
    }
}
